package flextestng.testing.xml
{
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	import flextestng.testing.model.ClassDefinition;
	import flextestng.testing.model.SuiteDefinition;
	import flextestng.testing.model.TestDefinition;
	
	public class XMLSuite
	{
		private var _suiteFile:File;
		
		public function XMLSuite(suiteFile:File)
		{
			_suiteFile = suiteFile;
		}
		
		final public function parse():SuiteDefinition
		{
			var stream:FileStream = new FileStream();
			stream.open(_suiteFile, FileMode.READ);
			
			var suiteXML:XML = new XML(stream.readUTFBytes(stream.bytesAvailable));
			
			var suite:SuiteDefinition = new SuiteDefinition(suiteXML.@name, suiteXML.@module);
			
			var parameters:XMLList = suiteXML.child("parameter");
			for each (var parameterXML:XML in parameters) {
				suite.addParamter(parameterXML.@name, parameterXML.@value);
			}
			
			var tests:XMLList = suiteXML.descendants("test");
			for each (var testXML:XML in tests) {
				var test:TestDefinition = new TestDefinition(testXML.@name, suite);
				
				parameters = testXML.child("parameter");
				for each (var testParameterXML:XML in parameters) {
					test.addParameter(testParameterXML.@name, testParameterXML.@value);
				}
				
				var includeGroups:XMLList = testXML.descendants("include");
				for each (var includeGroup:XML in includeGroups) {
					test.addIncludeGroup(includeGroup.@name);
				}
				
				var excludeGroups:XMLList = testXML.descendants("exclude");
				for each (var excludeGroup:XML in excludeGroups) {
					test.addExcludeGroup(excludeGroup.@name);
				}
				
				var classes:XMLList = testXML.descendants("class");
				for each (var classXML:XML in classes) {
					test.addTestClass( new ClassDefinition(classXML.@name, test.includedGroups, test.excludedGroups) );
				}
				
				suite.addTest(test);
			}
			
			stream.close();
			return suite;
		}
	}
}